//导入axios
import axios from "axios";
import { ElMessage } from "element-plus";
const Base_URL = "http://localhost:8080";
const instance = axios.create({ baseURL: Base_URL });

// import router from "@/router";
import { useRouter } from "vue-router";
//添加响应拦截器
instance.interceptors.response.use(
  (result) => {
    //判断业务状态吗
    if (result.data.status === "success" || result.data != null) {
      return result.data;
    } else if (result.status === "success" || result.status === 200) {
      return result.status;
    }
    return Promise.reject(result.data); //异步的状态转化成失败的状态
  },
  (err) => {
    confirm(`警告：${err.response.data.status}`);
    console.error(err.response.data);
    if (err.response && err.response.status === 401) {
      ElMessage.error("请先登录");
      const router = useRouter();
      router.push("/login");
    }
    // ElMessage.info("操作数据异常，操作取消");
    return Promise.reject(err); //异步的状态转化成失败的状态
  }
);

export default instance;
export { Base_URL };
